package com.hmdp.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisCreateId {
    private  StringRedisTemplate stringRedisTemplate;

    public RedisCreateId(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public long nextId(String keyPrefix){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime start = LocalDateTime.of(2025, 1, 1, 00, 00);
        long startSeconds = start.toEpochSecond(ZoneOffset.UTC);
        long nowSeconds = now.toEpochSecond(ZoneOffset.UTC);
        long timeStamp = nowSeconds-startSeconds;

        String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        Long id = stringRedisTemplate.opsForValue().increment("increment:" + keyPrefix + ":" + date);

        return timeStamp<<32 | id;
    }
}
